<template>
  <div class="log clearfix">
    <div class="white">
      <p>Welcome</p>
      <p>后台管理系统</p>
    </div>
    <div class="login">
      <h2>登录界面</h2>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <el-input type="text" v-model="ruleForm.captcha" autocomplete="off"></el-input>
        </el-form-item>
        <div class="captcha">
          <img :src="captcha" alt="" />
          <a href="javascript:;" @click="change"> 看不清？换一张</a>
        </div>
        <br />
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"> 提交 </el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      captcha: '',
      ruleForm: {
        password: '',
        username: '',
        captcha: '',
        key: ''
      },
      rules: {
        password: [{ validator: this.checkPassword, trigger: 'blur' }],
        username: [{ validator: this.checkUsername, trigger: 'blur' }],
        captcha: [{ validator: this.checkCaptcha, trigger: 'blur' }]
      }
    }
  },
  methods: {
    checkUsername(rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    },
    checkPassword(rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPassword !== '') {
          this.$refs.ruleForm.validateField('checkPassword')
        }
        callback()
      }
    },
    checkCaptcha(rule, value, callback) {
      if (!value) {
        return callback(new Error('验证码不能为空'))
      } else {
        callback()
      }
    },
    // 将对象转换成字符串
    objToStr(obj) {
      const arr = []
      for (const attr in obj) {
        arr.push(attr + '=' + obj[attr])
      }
      return arr.join('&')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 转换数据格式
          const data = this.objToStr(this.ruleForm)
          this.$http.post('/api/login', data).then((res) => {
            if (res.errNo === 0 && res.message === 'success') {
              // 请求成功 - 跳转页面
              this.$router.push('/dashboard/welcome')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 验证码的切换
    change() {
      this.$http.get('/api/captcha/math').then((res) => {
        this.captcha = res.img
        this.ruleForm.key = res.key
      })
    }
  },
  created() {
    // 获取验证码
    this.change()
  }
}
</script>

<style scoped lang="scss">
.log {
  width: 100%;
  height: 100%;
  margin: auto;
  background-image: url(../assets/bg.jpg);
  background-size: cover;
  padding-top: 50px;
  div {
    float: left;
  }
  .white {
    width: 500px;
    height: 500px;
    margin-left: calc((100% - 1000px) / 2);
    margin-top: 100px;
    margin-bottom: 71px;
    box-sizing: border-box;
    padding-top: 128px;
    background-color: rgba(119, 93, 113, 0.6);
    p {
      font-weight: bold;
      font-size: 40px;
      color: cornsilk;
      text-align: center;
    }
  }
  .login {
    margin-top: 100px;
    width: 500px;
    height: 500px;
    color: rgb(230, 229, 247);
    background-color: rgba(255, 255, 255, 0.5);
    h2 {
      text-align: center;
      color: #606266;
    }
    .el-form-item__content {
      margin-left: 150px;
    }
    .captcha {
      margin-left: 139px;
    }
    .el-form-item {
      margin-top: 30px;
      margin-left: 38px;
    }
    .el-input {
      width: 120%;
    }
    .el-button {
      position: relative;
      left: 44px;
    }
    ::v-deep.el-form-item__label {
      font-weight: bold;
    }
  }
}
.clearfix::after {
  display: block;
  content: '';
  clear: both;
}
</style>
